<?php
	namespace Qwadmin\Controller;
	use Think\Controller;
	class UnitsofmeasureController extends ComController{
		public $udb;
		public function __construct(){
			parent::__construct();
			$this->udb=M('Unitsofmeasure');
		}
		public function index(){
			$info=$this->udb->select();
			$count=count($info);
			$Page= new \Think\Page($count,25);
        	if($_GET['p']<1){
            	$_GET['p']=1;
        	}else{
            	$_GET['p']=(int)$_GET['p'];//
        	}
        	$list=array_slice($info, 25*($_GET['p']-1),25);
        	$show=$Page->show();
        	$this->assign('page',$show);
			$this->assign("info",$list);
			$this->display();
		}
		public function add_one(){
			$name=I('get.name');
			if($name){
				if($this->jugg($name)){
					$re['status']=false;
					$re['con']="单位重复";
				}else{
					$add['unitname']=$name;
					$re['status']=$this->udb->add($add);
					if($re['status']){
						$re['con']['name']=$name;
						$re['con']['id']=$re['status'];
					}else{
						$re['con']="未知错误";
					}
				}
			}else{
				$re['status']=false;
				$re['con']="输入值为空";
			}
			$this->ajaxReturn($re);
		}
		protected function jugg($name){
			$where['unitname']=$name;
			return $this->udb->where($where)->select();
		} 
		public function change_one(){
			$id=(int)I('post.id');
			$name=I('post.name');
			if($this->is_valid_id($id)){
				if($name){
					if($this->jugg($name)){
						$re['status']=false;
						$re['con']="单位重复";
					}else{
						$save['unitname']=$name;
						$save['unitid']=$id;
						$re['status']=$this->udb->save($save);
						if($re['status']){
							$re['con']['id']=$id;
							$re['con']['name']=$name;
						}else{
							$re['con']="操作错误";
						}
					}
				}else{
					$re['status']=false;
					$re['con']="输入值为空";
				}
			}else{
				$re['status']=false;
				$re['con']="不存在该信息！";
			}
			$this->ajaxReturn($re);
		}
		public function delete_one(){
			$id=(int)I('get.id');
			$re['status']=$this->udb->delete($id);
			$this->ajaxReturn($re);
		}
		public function get_id(){
			$id=(int)I('get.id');
			$ba=$this->udb->find($id);
			if($ba){
				$re['status']=true;
				$re['con']=$ba;
			}else{
				$re['status']=false;
			}
			$this->ajaxReturn($re);
		}
		public function is_valid_id($id){
			return $this->udb->find($id);
		}
		public function uid(){
			$uid=$_SESSION['uid'];
			$uid=(int)$uid;
			$info=M('Member')->find($uid);
			return $info['user'];
		}
		public function achieve(){
			$all=$this->udb->select();
			$this->ajaxReturn($all);
		}
	}